<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Product extends CI_Controller {

	public function index(){
	   $this->load->model('Products', null, true);
       $data['Products'] = $this->Products->getList();
		$this->load->view('product/index', $data);
	}

    /**
     * Action add
     * @author tien.nguyen
     */
    public function add(){
        $data = array();
        $this->load->model('Products', null, true);
        $this->load->model('Categories', null, true);
        
        //add product
        if($this->input->post()){
            $data = $this->input->post();
             //upload file
            $resUpload = $this->_uploadFile('Image');
            if(!is_null($resUpload)){       
                $data['Image'] = $resUpload['file_name'];
                if($this->Products->add($data)){
                    redirect(base_url().'product.html');
                }
            }
            $data['Product'] = $data;
        }        
        $data['OptionCategories'] = $this->Categories->getOptions();
        $this->load->view('product/add', $data);   
    }
    
    /**
     * Delete product
     * @author tien.nguyen
     */
    public function delete($productId){
        $this->load->model('Products', null, true);
        if($this->Products->delete($productId)){
            redirect(isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : base_url().'product.html' );
        }
    }
    
    /**
     * edit save config id
     * @author tien.nguyen
     */
    public function edit($productId){
        $res = false;
        $this->load->model('Products', null, true);
        $this->load->model('Categories', null, true);
        
        if($this->input->post()){
            $data = $this->input->post();
            //upload file
            if($_FILES['Image']['name'] != ''){
                $resUpload = $this->_uploadFile('Image');
                if(!is_null($resUpload)){       
                    $data['Image'] = $resUpload['file_name'];
                    if($this->Products->edit($data, $productId)){
                        redirect(base_url().'product.html');          
                    }    
                }
            }else{
                if($this->Products->edit($data, $productId)){
                    redirect(base_url().'product.html');          
                }    
            }
        }
        
        $data['Product'] = $this->Products->getProduct($productId);
        $data['OptionCategories'] = $this->Categories->getOptions();
        $this->load->view('product/edit', $data);       
    }
    
    /**
     * upload file 
     * @author tien.nguyen
     */
    private function _uploadFile($fileName){
       $config['upload_path'] = $this->config->item('base_upload').'/products';;
       $config['allowed_types'] = 'gif|png|jpg';
       $config['max_size'] = '1000';
       $config['max_width'] =  '3000';
       $config['max_height'] = '3000';
       
       $this->load->library('upload', $config);
       if(!$this->upload->do_upload($fileName)){
            $this->session->set_flashdata('message', $this->upload->error_msg);
            return null;
       }else{
            return $this->upload->data();
       }
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */